package cn.arikeky.user.service.impl;

import cn.arikeky.basic.config.BussinessException;
import cn.arikeky.basic.constant.BaseConstant;
import cn.arikeky.basic.dto.EmailDto;
import cn.arikeky.basic.util.MD5Utils;
import cn.arikeky.basic.util.StrUtils;
import cn.arikeky.org.service.impl.EmployeeServiceImpl;
import cn.arikeky.basic.dto.RegisterDto;
import cn.arikeky.user.domain.Logininfo;
import cn.arikeky.user.domain.User;
import cn.arikeky.user.mapper.LogininfoMapper;
import cn.arikeky.user.mapper.UserMapper;
import cn.arikeky.user.service.IUserService;
import cn.arikeky.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author arikeky
 * @since 2022-10-20
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeServiceImpl employeeService;

    public User registerVoEmail(EmailDto emailDto){
        User user = new User();
        user.setEmail(emailDto.getEmail());
        user.setUsername(emailDto.getEmail());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(emailDto.getPassword() + salt);
        user.setPassword(md5Pwd);
        user.setSalt(salt);
        return user;
    }

//    public static void main(String[] args) {
//        String salt = MD5Utils.encrypByMd5(StrUtils.getComplexRandomString(32));
//        String md5Pwd = MD5Utils.encrypByMd5("1"+salt);
//        System.out.println(salt+"----------------" +md5Pwd);
//    }

    public User registerVoUser(RegisterDto registerVo){
        User user = new User();
        user.setPhone(registerVo.getPhone());
        user.setUsername(registerVo.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(registerVo.getPassword() + salt);
        user.setPassword(md5Pwd);
        user.setSalt(salt);
        return user;
    }
    public Logininfo userLogininfo(User user){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);

        return logininfo;
    }

    @Override
    public void registerPhone(RegisterDto registerVo) {

        // todo 校验参数非空
        // 判断两次密码是否一致
        if (!registerVo.getPassword().equals(registerVo.getConfirmPassword())){
            throw new BussinessException("两次密码不一致，请重新输入！");
        }
        String smsCodeKey = BaseConstant.VerifyCodeConstant.BUSINESS_REGISTER_PREFIX + registerVo.getPhone();
        Object smsCodeValueTmp = redisTemplate.opsForValue().get(smsCodeKey);
        if (smsCodeValueTmp == null){
            throw new BussinessException("验证码已过期，请重新输入！");
        }
        // 判断短信验证码是否一致
        String[] split = smsCodeValueTmp.toString().split(":");
        String smsCode = split[0];
        if (!smsCode.equalsIgnoreCase(registerVo.getSmsCodeValue())){
            throw new BussinessException("验证码错误请重新输入！");
        }
        // 保存user logininfo 数据
        User user = registerVoUser(registerVo);
        Logininfo logininfo = userLogininfo(user);
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);

    }

    @Override
    public void registerEmail(EmailDto emailDto) {
        // 判断两次密码是否一致
        if (!emailDto.getPassword().equals(emailDto.getPasswordRepeat())){
            throw new BussinessException("两次密码不一致，请重新输入！");
        }
        Object emailCodeValueTmp = redisTemplate.opsForValue().get("email:code"+emailDto.getEmail());
        if (emailCodeValueTmp == null){
            throw new BussinessException("验证码已过期，请重新输入！");
        }
        // 判断短信验证码是否一致
        String[] split = emailCodeValueTmp.toString().split(":");
        String smsCode = split[0];
        if (!smsCode.equalsIgnoreCase(emailDto.getSmsCode())){
            throw new BussinessException("验证码错误请重新输入！");
        }
        // 保存user logininfo 数据
        User user = registerVoEmail(emailDto);
        Logininfo logininfo = userLogininfo(user);
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }

}
